/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package twi.result.graph;

import java.util.Iterator;
import prefuse.data.Edge;
import prefuse.data.Graph;
import prefuse.data.Node;
import prefuse.util.GraphLib;

/**
 *
 * @author Soeren
 */
public class PrefuseGraphFactory {

    public static Graph create(RGraph rg) {
       Graph g = new Graph(false);
		g.getNodeTable().addColumns(GraphLib.LABEL_SCHEMA);
		g.getEdgeTable().addColumns(GraphLib.LABEL_SCHEMA);
		
//		Node root = g.addNode();
//		root.setString(GraphLib.LABEL, "root");
		
		Iterator<RNode> nodeIter = rg.getNodeIter();
		while(nodeIter.hasNext()){
			RNode node = nodeIter.next();
			Node pn = g.addNode();
			pn.setString(GraphLib.LABEL, node.getName());
			node.setVisualNode(pn);
//			if (node.getInEdge().isEmpty())
//				g.addEdge(root, pn);
		}
		
		Iterator<REdge> eIter = rg.getEdgeIter();
		while(eIter.hasNext()){
			REdge e = eIter.next();
			Node n1 = e.getOrigin().getVisualNode();
			Node n2 = e.getTarget().getVisualNode();
                        if (n1 == null || n2 == null){
//                            System.err.println("Kein Knoten");
                            continue;
                        }
			Edge edge = g.addEdge(n1, n2);
			String name = e.getLabel();
			if (!name.contains("->"))
				edge.setString(GraphLib.LABEL, name);				
		}
		return g;
    }

}
